package team.knowers.agency.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import team.knowers.agency.dao.entity.FlightDO;
import team.knowers.agency.dao.mapper.FlightMapper;
import team.knowers.agency.dto.req.GetFlightSearchReqDTO;
import team.knowers.agency.dto.req.PageGetFlightReqDTO;
import team.knowers.agency.service.FlightService;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;

@Service
@RequiredArgsConstructor
public class FlightServiceImpl extends ServiceImpl<FlightMapper, FlightDO> implements FlightService {
    private final FlightMapper flightMapper;

    @Override
    public List<FlightDO> getFlightPerPassenger(GetFlightSearchReqDTO reqDTO) {
        LocalDate startDate = reqDTO.getStartTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDateTime startDateTimeStart = startDate.atStartOfDay();
        LocalDateTime startDateTimeEnd = startDate.atTime(23, 59, 59);
        LambdaQueryWrapper<FlightDO> le = Wrappers.lambdaQuery(FlightDO.class).eq(FlightDO::getStartPlace, reqDTO.getStartPlace())
                .eq(FlightDO::getEndPlace, reqDTO.getEndPlace())
                .ge(FlightDO::getStartTime, startDateTimeStart)
                .le(FlightDO::getEndTime, startDateTimeEnd)
                .orderByAsc(FlightDO::getStartTime)
                .eq(FlightDO::getDelFlag,0);
        return flightMapper.selectList(le);
    }

    @Override
    public FlightDO getFlight(int id) {
        LambdaQueryWrapper<FlightDO> wrapper=Wrappers.lambdaQuery(FlightDO.class)
                .eq(FlightDO::getId,id)
                .eq(FlightDO::getDelFlag,0);
        FlightDO flightDO=baseMapper.selectOne(wrapper);
        return flightDO;
    }

    @Override
    public void modifyInformation(int id, int num,int seatReq) {
        LambdaQueryWrapper<FlightDO> wrapper=Wrappers.lambdaQuery(FlightDO.class)
                .eq(FlightDO::getId,id)
                .eq(FlightDO::getDelFlag,0);
        FlightDO flightDO=baseMapper.selectOne(wrapper);
        if(seatReq==1)
        {
            flightDO.setNowHeadSeats(flightDO.getNowHeadSeats()+num);
        }
        else
        {
            flightDO.setNowEcoSeats(flightDO.getNowEcoSeats()+num);
        }
        baseMapper.update(flightDO,wrapper);

    }
    @Override
    public IPage<FlightDO> pageFlight(PageGetFlightReqDTO req) {
        if(req.getEndPlace().isEmpty()&&req.getStartPlace().isEmpty()&&req.getFlightNo().isEmpty()) {
            return flightMapper.selectPage(req
                    , Wrappers.lambdaQuery(FlightDO.class).eq(FlightDO::getDelFlag, 0)
                            .between(FlightDO::getStartTime, LocalDateTime.now().plusHours(2),
                                    LocalDateTime.now().plusYears(1))
                            .orderByAsc(FlightDO::getFlightNo));
        }
        return flightMapper.pageFlight(req);

    }
}
